using System;
using System.Collections.Generic;
using System.Text;

namespace Emlyn.Common.Collections
{
    public class SimpleDictionary<TKey, TValue>: ISimpleDictionary<TKey, TValue>
        where TValue: IKeyable<TKey>
    {
        Dictionary<TKey, TValue> _dictionary;

        public SimpleDictionary()
        {
            _dictionary = new Dictionary<TKey, TValue>();
        }

        #region ISimpleDictionary<TKey,TValue> Members

        public void AddByValue(TValue aValue)
        {
            _dictionary.Add(aValue.GetKey(), aValue);
        }

        public void RemoveByValue(TValue aValue)
        {
            _dictionary.Remove(aValue.GetKey());
        }

        public void RemoveByKey(TKey aKey)
        {
            _dictionary.Remove(aKey);
        }

        public TValue GetByKey(TKey aKey)
        {
            return _dictionary[aKey];
        }

        public TValue GetByValue(TValue aValue)
        {
            return _dictionary[aValue.GetKey()];
        }

        public bool ContainsKey(TKey aKey)
        {
            return _dictionary.ContainsKey(aKey);
        }

        public bool ContainsValue(TValue aValue)
        {
            return _dictionary.ContainsValue(aValue);
        }

        #endregion

        #region ICollection<TValue> Members

        public void Add(TValue aValue)
        {
            AddByValue(aValue);
        }

        public void Clear()
        {
            _dictionary.Clear();
        }

        public bool Contains(TValue item)
        {
            return ContainsValue(item);
        }

        public void CopyTo(TValue[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get
            {
                return _dictionary.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(TValue item)
        {
            bool retval = false;

            if (Contains(item))
            {
                RemoveByValue(item);
                retval = true;
            }

            return retval;
        }

        #endregion

        #region IEnumerable<TValue> Members

        public IEnumerator<TValue> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _dictionary.GetEnumerator();
        }

        #endregion
    }
}
